<?php
class UserMessagesController extends AppController{
	var $name = 'UserMessages';
	var $uses = array('UserMessage','User');
	var $helpers = array('Html','Session');
	var $components = array('Session');

	public function beforeFilter(){
        parent::beforeFilter();
    }

    /*

    */
    public function newMessages(){
    	$newMessages = $this->UserMessage->find('all',array('conditions'=>array(
																				'UserMessage.to_user_id'=>$this->Auth->user('id'),
																				'UserMessage.view_flag'=>false)));
    	for($i=0; $i<count($newMessages); $i++){
    		$user = $this->User->findById($newMessages[$i]['UserMessage']['from_user_id']);
    		$newMessages[$i]['Profile'] = $user['Profile'];
    	}
    	return $newMessages;
    }

	/*
		Chức năng: hiển thị các user để chọn gửi tin nhắn
		Author: đã đăng nhập
	*/
	public function listUser(){
		$messages = $this->UserMessage->find('all',array(
													'fields'=>array('UserMessage.from_user_id'),
													'conditions'=>array(
														'to_user_id'=>$this->Auth->user('id')),
													'group'=>array('from_user_id','to_user_id'),
													'order'=>array('UserMessage.view_flag ASC')
													));
		//$userIdToMessages[] = $this->Auth->user('id');
		if($messages){
			foreach($messages as $message){
				$userIdToMessages[] = $message['UserMessage']['from_user_id'];
			}
		}
		
		$userSendMessages = $this->User->find('all',array(
													'conditions'=>array(
														'User.id'=>$userIdToMessages)));
		for($i=0; $i<count($userSendMessages); $i++){
			
			$userMessages = $this->UserMessage->find('all',array(
																'conditions'=>array(
																	'OR'=>array(
																		array(
																			'UserMessage.to_user_id'=>$this->Auth->user('id'),
																			'UserMessage.from_user_id'=>$userSendMessages[$i]['User']['id']
																			),
																		array(
																			'UserMessage.to_user_id'=>$userSendMessages[$i]['User']['id'],
																			'UserMessage.from_user_id'=>$this->Auth->user('id')
																			)
																		))

																));
			$newMessage = 0;

			foreach($userMessages as $userMessage){
				if( !($userMessage['UserMessage']['view_flag']) && 
					($userMessage['UserMessage']['to_user_id'] == $this->Auth->user('id')) ){
						$newMessage++;
					}
			}
			$userSendMessages[$i]['Message'] = $userMessages;
			$userSendMessages[$i]['newMessage'] = $newMessage;
		}
		//debug($userSendMessages);
		$userIdToMessages[] = $this->Auth->user('id');
		$userAthor = $this->User->find('all',array(
												'conditions'=>array(
													'NOT'=>array(
														'User.id'=>$userIdToMessages))));
		$this->set('userSendMessages',$userSendMessages);
		$this->set('userAthor',$userAthor);
	}

	/*
		Chức năng: gửi tin nhắn tới user có id là $toUserId
		Author: đã đăng nhập
	*/
	public function sendMessage($toUserId = null){
		if($this->request->is('post')){
			$this->UserMessage->create();
			$this->request->data['UserMessage']['from_user_id'] = $this->Auth->user('id');
			$this->request->data['UserMessage']['to_user_id'] = $toUserId;
			if($this->UserMessage->save($this->request->data)){
				//$this->Session->setFlash(__('Sended!'));
				return $this->redirect(array('action'=>'viewNewMessage',$toUserId));
			}
			$this->Session->setFlash(__('error_send'));
		}
	}

	public function viewFlag($userMessageId = null){
		if($userMessageId){
			$newMessage = $this->UserMessage->findById($userMessageId);
			$newMessage['UserMessage']['view_flag'] = 1;
			$this->UserMessage->save($newMessage);
		}
	}

	public function messageReaded($toUserId = null){
		$messages = $this->UserMessage->find('all', array(
													'conditions'=>array(
														'view_flag'=>0,
														'from_user_id'=>$toUserId)
													));
		if($messages){
			foreach($messages as $message){
				$this->viewFlag($message['UserMessage']['id']);
			}
		}
	}

	/*
		Chức năng: xem tin nhắn mới
		Author: đã đăng nhập
	*/
	public function viewNewMessage($toUserId = null){
		
		$this->messageReaded($toUserId);
		$userMessages = $this->UserMessage->find('all',array(
															'conditions'=>array(
																'OR'=>array(
																	array(
																		'UserMessage.to_user_id'=>$this->Auth->user('id'),
																		'UserMessage.from_user_id'=>$toUserId
																		),
																	array(
																		'UserMessage.to_user_id'=>$toUserId,
																		'UserMessage.from_user_id'=>$this->Auth->user('id')
																		)
																	))
																));
		$toUser = $this->User->findById($toUserId);
		$this->set('toUser',$toUser);
		$this->set('userMessages',$userMessages);
		
	}
	/*
		Chức năng: xem tất cả các tin nhắn
		Author: đã đăng nhập
	*/
	public function ViewAllMessages(){
		$userId = $this->Auth->user('id');
		
		$giveMessages = $this->UserMessage->find('all', array('conditions'=>array('UserMessage.to_user_id'=>$userId),
															  'order'=>array('UserMessage.view_flag'=>'desc')));
		$this->set('giveMessages',$giveMessages);

		$sendMessages = $this->UserMessage->find('all', array('conditions'=>array('UserMessage.from_user_id'=>$userId),
															 'order'=>array('UserMessage.view_flag'=>'desc')));
		$this->set('sendMessages',$sendMessages);
	}

	public function deleteMessage($userMessageId = null){
		if($userMessageId){
			if($this->UserMessage->delete($userMessageId)){
				$this->Session->setFlash(__('message id %s deleted', h($userMessageId)));
				return $this->redirect(array('action'=>'ViewAllMessages'));
			}
		}
	}
	
}
?>